import json

with open('data.json', 'r', encoding='utf-8') as file:
    data = json.loads(file.read())

currentEntry = 'A'

"""
data = [
    {'name': 'A', 'requires': ['B', 'D', 'E']},
    {'name': 'B', 'requires': ['F']},
    {'name': 'C', 'requires': ['G', 'H']},
    #{'name': 'D', 'requires': ['G']},       # F, B, G, D, E, A
    {'name': 'D', 'requires': ['G', 'C']},  # F, B, G, H, C, D, E, A
    {'name': 'E', 'requires': []},
    {'name': 'F', 'requires': []},theCurrentEntryIsResolved
    {'name': 'G', 'requires': []},
    {'name': 'H', 'requires': []}
]
"""

def theCurrentEntryIsResolved():
    return len(entries[-1]['requires']) > 0

def getTheFirstRequiresItemOfTheLastEntry():
    return entries[-1]['requires'][0]

def getTheNameOfTheLastEntry():
    return entries[-1]['name']

def deleteTheLastEntry():
    entries.pop(-1)

def deleteTheFirstRequiresItemOfTheLastEntry():
    entries[-1]['requires'].pop(0)

def deleteTheLastEntryAndAssignNewValueToCurrentEntry():
    deleteTheLastEntry()
                
    if theCurrentEntryIsResolved():
        currentEntry = getTheFirstRequiresItemOfTheLastEntry()
    else:
        currentEntry = getTheNameOfTheLastEntry()

dataDependenciesPath = []
entries = []

dataLength = len(data)
index = 0

while index < dataLength:
    # if the currentEntry is already resolved
    if currentEntry in dataDependenciesPath:
        if theCurrentEntryIsResolved():
            deleteTheFirstRequiresItemOfTheLastEntry()

            if theCurrentEntryIsResolved():
                currentEntry = getTheFirstRequiresItemOfTheLastEntry()
            else:
                deleteTheLastEntryAndAssignNewValueToCurrentEntry()
        else:
            deleteTheLastEntryAndAssignNewValueToCurrentEntry()
    else:
        if data[index]['name'] is currentEntry:
            entries.append(data[index])

            if theCurrentEntryIsResolved():
                currentEntry = getTheFirstRequiresItemOfTheLastEntry()
                            
                index = 0
                continue
            else:
                dataDependenciesPath.append(currentEntry)
                deleteTheLastEntry()
                deleteTheFirstRequiresItemOfTheLastEntry()

                if theCurrentEntryIsResolved():
                    currentEntry = getTheFirstRequiresItemOfTheLastEntry()

                    index = 0
                    continue
                else:
                    dataDependenciesPath.append(getTheNameOfTheLastEntry())
                    deleteTheLastEntry()
                
                # if there are more entries to be resolved
                if entries:
                    deleteTheFirstRequiresItemOfTheLastEntry()
                
                    if theCurrentEntryIsResolved():
                        currentEntry = getTheFirstRequiresItemOfTheLastEntry()
                    else:
                        currentEntry = getTheNameOfTheLastEntry()
                        deleteTheLastEntry()
                else:
                    break
                
            index = 0
            continue

        index += 1

print(dataDependenciesPath)